package com.java.dao;


import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import com.google.common.collect.Lists;
import com.java.vo.PointSignRecordVo;


@Repository
public class PointSignDao {

	private Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	
	public List<PointSignRecordVo> findAllSignDayRecord(Integer userId,Integer page,String signTime) {
		ArrayList<Object> parmList = new ArrayList<Object>();
		StringBuffer sql = new StringBuffer(" SELECT sd.*, sc.continue_count,sc.total_count,sc.last_sign_date "
				+ "FROM point_sign_record sd "
				+ " LEFT JOIN  point_sign_user sc ON sd.user_id = sc.user_id WHERE 1=1");
		if (!StringUtils.isEmpty(userId)) {
			sql.append(" AND sd.user_id = ? ");
			parmList.add(userId);
		}
		
		if (!StringUtils.isEmpty(signTime) && !signTime.equals("-")) {
			String[] sAry = formatPeriod(signTime).split("-");
			if (!sAry[0].equals("#")) {
				sql.append(" AND sd.time >= ? ");
				parmList.add(sAry[0]);
			}
			if (!sAry[1].equals("#")) {
				sql.append(" AND sd.time <= ? ");
				parmList.add(sAry[1]+"235959999");
			}
		}
		
		if(parmList.size() == 0 && sql.toString().endsWith("WHERE 1=1")){
			return new ArrayList<PointSignRecordVo>();
		}
		
		if (page != null) {
			sql.append(" ORDER BY sd.id DESC LIMIT ").append(10 * (page - 1)).append(",").append(10);
		}
		
		System.out.println(sql.toString());
		try{
			List<PointSignRecordVo> recordVoList = jdbcTemplate.query(sql.toString(),
					parmList.toArray(new Object[parmList.size()]), (rs) -> {
						List<PointSignRecordVo> list = Lists.newArrayList();
						while (rs.next()) {
							PointSignRecordVo recordVo = new PointSignRecordVo();
							recordVo.setUserId(rs.getInt("user_id"));
							recordVo.setTime(rs.getTimestamp("time"));
							recordVo.setLastSignDate(rs.getTimestamp("time"));
							recordVo.setTotalCount(rs.getInt("total_count"));
							recordVo.setContinueCount(rs.getInt("continue_count"));
							list.add(recordVo);
						}
						return list;
					});
			return recordVoList;
		}catch(Exception e){
			e.printStackTrace();
			logger.error("shop-findAllSignDayRecord error" + e.getMessage());
			return null;
		}
	}
	
	private String formatPeriod(String periodStr) {
		StringBuffer sb = new StringBuffer(periodStr);
		if (periodStr.startsWith("-")) {
			sb.insert(0, "#");
		}
		
		if (periodStr.endsWith("-")) {
			sb.append("#");
		}
		
		return sb.toString();
	}
	
	
	public Integer getSignDayRecordCnt(Integer userId,String signTime) {
		ArrayList<Object> parmList = new ArrayList<Object>();
		StringBuffer sql = new StringBuffer(" SELECT COUNT(1) cnt "
				+ "FROM point_sign_record sd "
				+ " LEFT JOIN  point_sign_user sc ON sd.user_id = sc.user_id WHERE 1=1");
		if (!StringUtils.isEmpty(userId)) {
			sql.append(" AND sd.user_id = ? ");
			parmList.add(userId);
		}
		
		if (!StringUtils.isEmpty(signTime) && !signTime.equals("-")) {
			String[] sAry = formatPeriod(signTime).split("-");
			if (!sAry[0].equals("#")) {
				sql.append(" AND sd.time >= ? ");
				parmList.add(sAry[0]);
				System.out.println("sAry[0]－－－－" + sAry[0]);
			}
			if (!sAry[1].equals("#")) {
				sql.append(" AND sd.time <= ? ");
				parmList.add(sAry[1]+"235959999");
				System.out.println("--------" + sAry[1]+"235959999");
			}
		}
		
		if(parmList.size() == 0 && sql.toString().endsWith("WHERE 1=1")){
			return 0;
		}
		System.out.println("sql-----" + sql.toString());
		System.out.println("parmList-----" + parmList.toString());
		try{
			Integer count = jdbcTemplate.query(sql.toString(), parmList.toArray(new Object[parmList.size()]), (rs) -> {
				Integer i = null;
				while (rs.next()) {
					i = rs.getInt("cnt");
				}
				return i;
			});
			return count;
		}catch(Exception e){
			e.printStackTrace();
			logger.error("shop-getSignDayRecordCnt error" + e.getMessage());
			return 0;
		}
	}
	
}